<template>
  <div class="container">
    <!--  -->
    <div class="title">
      <div v-show="$route.query.id" style="margin-bottom:20px">

        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>标签</el-breadcrumb-item>
          <el-breadcrumb-item>学科名称</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
        <hr style="color: red ">
      </div>

      <el-form class="form" style="display:flex;justify:start;">
        <el-form-item class="item" label="标签名称">
          <el-input v-model="queryParams.tagName" />
        </el-form-item>

        <el-form-item class="item item2" label="状态">
          <el-select v-model="queryParams.state">
            <el-option :value="1" label="启用" />
            <el-option :value="0" label="禁用" />
          </el-select>
        </el-form-item>
        <el-form-item class="item item2">
          <el-button size="mini" @click="reset">清除</el-button>
          <el-button type="primary" size="mini" @click="btnSearch">搜索</el-button>
        </el-form-item>
        <!-- 返回学科 -->
        <el-form-item style="margin-left: 370px">
          <el-button v-show="$route.query.id" style="margin-right: 20px" type="text" size="mini" icon="el-icon-arrow-left" @click="$router.push('/subjects/list')">返回学科</el-button>
          <el-button type="success" icon="el-icon-edit" size="mini" @click="btnAddEdit()">新增标签</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据一共多少条 -->
      <div>
        <el-alert
          :title="`数据一共${total}条`"
          type="info"
          show-icon
        />
      </div>

      <!-- 表格 -->
      <div>
        <el-table :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="所属学科">
            <template v-slot="{row}">
              {{ row.subjectName }}
            </template>
          </el-table-column>
          <el-table-column label="标签名称">
            <template v-slot="{row}">
              {{ row.tagName }}
            </template>
          </el-table-column>
          <el-table-column label="创建者">
            <template v-slot="{row}">
              {{ row.username }}
            </template>
          </el-table-column>
          <el-table-column label="创建日期">
            <template v-slot="{row}">
              {{ row.addDate | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="状态">
            <template v-slot="{row}">
              <span v-if="row.state === 1">已启用</span>
              <span v-else>已禁用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button type="text" @click="btnOff(row)">{{ row.state === 1 ? '禁用' : '启用' }}</el-button>
              <el-button type="text" :disabled="row.state === 1 ? true : false" @click="btnAddEdit(row)">修改</el-button>
              <el-button type="text" :disabled="row.state === 1 ? true : false" @click="btnDel(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" style="height:60px;" align="middle" justify="end">
          <el-pagination

            background
            layout="prev, pager, next,sizes,jumper"
            :page-sizes="[5, 10, 20, 50]"
            :total="total"
            :page-size="queryParams.pagesize"
            :current-page="queryParams.page"
            @current-change="changePage"
            @size-change="changeSize"
          />
        </el-row>
      </div>

    </div>
    <!-- 新增/编辑 弹出层 -->
    <el-dialog
      :title="showText"
      :visible.sync="showTags"
      width="35%"
      @close="close"
    >
      <el-form ref="formData" label-width="120px" :model="formData" :rules="rules">
        <el-form-item v-show="$route.query.id ? false : true" label="所属学科" prop="subjectID">
          <el-select v-model="formData.subjectID" style="width:90%">
            <el-option v-for="item in subjectList" :key="item.value" :label="item.label" :value="item.value">
              <!-- {{item.label}} -->
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="tagName">
          <el-input v-model="formData.tagName" style="width:90%" placeholder="请输入标签名称" />
        </el-form-item>
      </el-form>
      <el-row type="flex" style="height:60px;" align="middle" justify="end">
        <el-col :span="8">
          <el-button size="mini" @click="close">取消</el-button>
          <el-button type="primary" size="mini" @click="btnOk">确认</el-button>
        </el-col>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
import { list, add, detail, update, remove, changeState } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'
import moment from 'moment'

export default {
  name: 'Tags',
  // 格式化时间
  filters: {
    formatDate(val) {
      return moment(val).format('YYYY-MM-DD')
    }
  },

  data() {
    return {
      // 弹出层
      showTags: false,
      list: [],
      // 定义分页的查询参数
      queryParams: {
        page: 1, // 当前页码
        pagesize: 10,
        state: null,
        tagName: '',
        subjectID: +this.$route.query.id || null

      },
      pages: null, // 总页数
      // 总条数
      total: null,

      formData: {
        subjectID: +this.$route.query.id || null,
        tagName: ''
      },
      rules: {
        subjectID: [{ required: true, message: '请选择所属学科', trigger: 'blur' }],
        tagName: [{ required: true, message: '请输入标签名称', trigger: 'blur' }]
      },
      // 学科列表
      subjectList: [],
      tag: this.$route.query.id

    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑标签' : '新增标签'
    }
  },
  watch: {
    tag() {
      this.getTagsList()
    }
  },
  created() {
    this.getTagsList()
  },
  methods: {
    // 获取标签列表
    async getTagsList() {
      const { data } = await list(this.queryParams)
      console.log(data)
      this.list = data.items
      this.total = +data.counts
      // this.queryParams.page = +data.page
      this.queryParams.pagesize = +data.pagesize
      this.pages = +data.pages
      // console.log(await list())
    },
    // 页码发生变化是调用
    changePage(newPage) {
      console.log(newPage)
      this.queryParams.page = newPage
      this.getTagsList()
    },
    // 每页显示条数变化时
    changeSize(pageSize) {
      console.log(pageSize)
      this.queryParams.pagesize = pageSize
      this.getTagsList()
    },
    // 点击 添加/编辑
    async btnAddEdit(row) {
      // 拿到所属学科列表
      const { data } = await simple()
      // console.log(data)
      this.subjectList = data
      if (row) {
        // 修改
        console.log(await detail(row))
        // 拿到数据，渲染到表格上
        const { data } = await detail(row)
        this.formData = data
      }

      // 打开弹层
      this.showTags = true
    },
    // 点击弹出层的 确认按钮
    btnOk() {
      this.$refs.formData.validate(async isOk => {
        if (isOk) {
          if (this.formData.id) {
            // 修改
            // 调用修改接口
            await update(this.formData)
            this.$message.success('修改标签成功')
          } else {
            // 新增
            await add(this.formData)
            this.$message.success('新增标签成功')
          }
          this.close()
          this.getTagsList()
        }
      })
    },
    // 点击弹出层的 取消按钮
    close() {
      // 重置表单
      this.$refs.formData.resetFields()
      this.showTags = false
    },
    // 点击删除
    btnDel(row) {
      this.$confirm('此操作将永久删除该便签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        if (this.list.length === 1 && this.queryParams.page > 1) {
          this.queryParams.page--
        }
        await remove(row)
        this.$message.success('删除目录成功')
        this.getTagsList()
      }).catch(() => {
        //
      })
    },
    // 点击禁用/启用 按钮
    async btnOff(row) {
      await changeState({
        id: row.id,
        state: row.state === 1 ? 0 : 1
      })
      row.state = row.state === 1 ? 0 : 1
      this.$message.success('操作成功')
      // this.getTagsList()
    },
    // 点击清除按钮
    reset() {
      this.queryParams = {
        state: null,
        tagName: ''
      }
      // this.$refs.formqueryParams.resetFields()
      this.queryParams.page = 1
      this.getTagsList()
    },
    // 点击搜索按钮
    async btnSearch() {
      this.queryParams.page = 1
      this.getTagsList()
    }

  }
}
</script>

<style scoped lang='scss'>
.container{
  margin: 10px;
  background-color: #fff;

  .title{
    margin: 10px;
    padding-top: 20px;
    .item{
      margin-left: 10px;
      display: flex;
      justify: start
    }
    .item2 {
      padding-left: 20px;

    }
    .item3 {
      // display: flex;
      // justify: start;
      margin-left: 100px;
    }
  }
}
</style>
